<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/flash/generatepagelist.php
#	FUNCTION:		Generates an XML document of pages in the current site
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Report all errors
error_reporting(E_ALL);

# Start session
session_start();

# Use this for IE to remember form data correctly
header("Cache-control: private");

# Vars
$ERROR_ARRAY = array();
$SQL_ARRAY = array();

# XML
header("content-type: text/xml");

# Includes
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/text.php";
include "../includes/files.php";


# Establish database connection
establishDatabaseConnection();


getXMLPages();
function generatePage($current_pages = array(), $parent_id = ""){
	global $xmlContent;
	foreach ($current_pages as $current_page_id => $current_page) {
		
		if ($current_page["p_parent"] == $parent_id) {
					
				$xmlContent.="<page id=\"".$current_page["p_id"]."\" name=\"".$current_page["p_name"]."\">";
				$xmlContent.="<content><![CDATA[".parseFlashLinks(parseFlashXML($current_page["p_content"]))."]]></content>";			#parseFlashLinks(parseFlashXML($current_page["p_content"]))
				$xmlContent.="<pages>";
				generatePages($current_pages, $current_page["p_id"],false);
				$xmlContent.="</pages>";
				$xmlContent.="</page>";
		}	
	}
}

function generatePages($current_pages = array(), $parent_id = "",$firstTime) {

	global $ROW_COUNT;
	global $xmlContent;
	if (!$ROW_COUNT) {
		$ROW_COUNT = 0;
	}
	
	foreach ($current_pages as $current_page_id => $current_page) {
		if($firstTime==true){
			if ($current_page["p_parent"] == $parent_id && $current_page["p_innavigation"] == "1") {
						
					$xmlContent.="<page id=\"".$current_page["p_id"]."\" name=\"".$current_page["p_name"]."\">";
					$xmlContent.="<content><![CDATA[".parseFlashLinks(parseFlashXML($current_page["p_content"]))."]]></content>";			#parseFlashLinks(parseFlashXML($current_page["p_content"]))
					$xmlContent.="<pages>";
					$ROW_COUNT++;
					generatePage($current_pages, $current_page["p_id"]);
					$xmlContent.="</pages>";
					$xmlContent.="</page>";
			}
		}
		else{
			if ($current_page["p_parent"] == $parent_id) {			  
					$xmlContent.="<page id=\"".$current_page["p_id"]."\" name=\"".$current_page["p_name"]."\">";
					$xmlContent.="<content><![CDATA[".parseFlashLinks(parseFlashXML($current_page["p_content"]))."]]></content>";			#parseFlashLinks(parseFlashXML($current_page["p_content"]))
					$xmlContent.="<pages>";
					$ROW_COUNT++;
					generatePage($current_pages, $current_page["p_id"]);
					$xmlContent.="</pages>";
					$xmlContent.="</page>";
			}

		}
	}
}

function getXMLPages(){
	$xmlContent="";
	$sql = "SELECT p_id, p_name, p_parent, p_section, p_sort,p_content,p_innavigation FROM mrt_pages WHERE p_status<3 AND p_section='site' "." ORDER BY p_sort ASC, p_parent ASC";
	$result_sql=getRecord($sql);
	generatePages($result_sql, "",true);
	global $xmlContent;
	echo "<pages>".$xmlContent."</pages>";
	
}

?>